"""变压器桥式电容传感电路"""
# 1.电路的测量功能：自变量和输出的关系
# 2.参数变化、噪声和补偿手段的关系
from math import floor
import numpy as np
from scipy.integrate import solve_ivp
from system_odes import Bridge

k = 1.38e-12# Boltzmann constant
on_off = np.array([1e8, 1])
switching = on_off[[0,0,1,0,0,1]]
switch_states = [0 ,0, 1]

# -------------------系统数值积分测试------------------------------
# states = [Ip1 Ip2 Is 
#           Qceq1 Qceq2 
#           Qpri11 Qpri12 Qpri13 # 假设一个臂用3个补偿电容
#           Qpri21 Qpri22 Qpri23 
#           Uo1 dUo1_dt Uo2 dUo2_dt Ulp dUlp_dt]'
# 系统参数
on_off = [1e8, 1]
switching = np.array([
    on_off[0], on_off[0], on_off[1],
    on_off[0], on_off[0], on_off[1]], dtype=np.float64)
C_pi = np.array([2e-12, 4e-12, 8e-12, 2e-12, 4e-12, 8e-12], dtype=np.float64)
C_p = np.array([243.3e-12, 243.3e-12], dtype=np.float64)
brg_rc3 = Bridge(switching, C_pi, C_p)
# 积分参数
state_init = np.zeros((17,), dtype=np.float64)
fs = 4e3
T_span = 0, 8# 运行8秒约需要18h
num = int((T_span[-1] - T_span[0])*fs + 1)
t_eval = np.linspace(T_span[0], T_span[-1], num)
a_tol = np.array(\
    [1e-8,1e-8,1e-12,\
    1e-14,1e-14,\
    1e-14,1e-14,1e-14,\
    1e-14,1e-14,1e-14,\
    1e-7,1e-1,1e-7,1e-1,1e-6,1e-7],dtype=np.float)# default: 1e-6
r_tol = np.ones((17,),dtype=np.float)# default: 1e-3
r_tol *= 1e-3
# 'BDF'是隐式多步变阶（1~5）积分方法，提供jacobian()可以提高精度和效率，但还是挺慢
results = solve_ivp(brg_rc3.bridge_odes, T_span, state_init, method='BDF',\
    t_eval=t_eval, atol=a_tol, rtol=1e-3, jac=brg_rc3.jacobian)
print(results.nfev)# 416574,313967696
file_path = 'rc_baseline.npz'
np.savez(file_path, T=results.t, Y=results.y)
